2020-2021 Sunseeker Telemetry and Lighting System
timer_a_ex2_singlePWM.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx Timer_A - Single PWM
34  *
35  * Description: In this example, the 16 bit Timer_A is configured to output a
36  * 75% duty cycle PWM on P1.5. The timer uses SMCLK / 4 for a clock source and
37  * outputs a PWM signal with a 8ms period. The PWM generation does not require
38  * any interrupts and stays in LPM0. Calculations for the period and duty
39  * cycle can be seen below.
40  *
41  * MSP430i2041
42  * ------------------
43  * /|\| |
44  * | | |
45  * --|RST P1.5|--->75% Duty Cycle PWM
46  * | |
47  * | |
48  * | |
49  *
50  *
51  * Author: Zack Lalanne
52  ******************************************************************************/
53 #include "driverlib.h"
54 
55 // 8ms Period Calculation:
56 // SMCLK = DCO = 16.384 MHz
57 // TACLK = SMCLK / 4 = 4.096 MHz
58 // 8ms / (1 / 4.096MHz) = 32768
59 #define TIMER_PERIOD 32768
60 
61 // 75% Duty Cycle Calculation:
62 // TIMER_PERIOD * 0.75 = 24576
63 #define TIMER_DUTY_CYCLE 24576
64 
65 int main(void) {
66 
67  Timer_A_outputPWMParam outputPWMConfig = {
68  TIMER_A_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
69  TIMER_A_CLOCKSOURCE_DIVIDER_4, // SMCLK/4 = 4.096 MHz
70  TIMER_PERIOD, // 8ms
71  TIMER_A_CAPTURECOMPARE_REGISTER_1, // Output on TA0.1
72  TIMER_A_OUTPUTMODE_RESET_SET, // Generate PWM
73  TIMER_DUTY_CYCLE // 75% Duty Cycle
74  };
75 
76  WDT_hold(WDT_BASE);
77 
78  // Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
79  // SMCLK = DCO = 16.384MHz
80  CS_setupDCO(CS_INTERNAL_RESISTOR);
81  CS_initClockSignal(CS_SMCLK, CS_CLOCK_DIVIDER_1);
82 
83  // Setting up P1.5 as TA0.1 for PWM signal generation
84  GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P1,
85  GPIO_PIN5,
86  GPIO_SECONDARY_MODULE_FUNCTION);
87 
88  // Configure the timer to use ACLK and interrupt on overflow
89  Timer_A_outputPWM(TIMER_A0_BASE, &outputPWMConfig);
90 
91  // Go into LPM0 with interrupts enabled
92  __bis_SR_register(LPM0_bits);
93 }
#define TIMER_DUTY_CYCLE
int main(void)
#define TIMER_PERIOD